﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using SUMO.MediaBrowser.ViewModel;
using SUMO.MediaBrowser.Foundation;
using USTF.Media.MediaService;
using USTF.Media.MediaServiceConsumer.USTFMediaServiceProxy;

namespace SUMO.MediaBrowser.Controls
{
    public class MediaTreeViewItem : TreeViewItem
    {
        public HierarchicalMediaItem _carrier;
        public HierarchicalMediaItem Carrier
        {
            get
            {
                return _carrier;
            }
            set
            {
                _carrier = value;
                _carrier.ChildItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ChildItems_CollectionChanged);
            }
        }

        public MediaTreeViewItem()
            : base()
        {
            this.Header = "Umbraco Media";
            
           // this.Items.Add(new LoadingTreeViewItem());

            this.Expanded += new RoutedEventHandler(OnExpanded);

            //todo: property data binding (without xaml :-( )
            //this.Header = Carrier.CurrentItem.Properties.ContainsKey("Title") ? Carrier.CurrentItem.Properties["Title"] : "no-title";

        }


        public MediaTreeViewItem(HierarchicalMediaItem m)
            : this()
        {
            this.Carrier = m;
            if (Carrier != null && Carrier.CurrentItem != null)
                this.Header = Carrier.CurrentItem.Properties.ContainsKey("Title") ? Carrier.CurrentItem.Properties["Title"] : "Media";
            else this.Header = "[item not set]";
        }


        /// <summary>
        /// When the items we are representing are modified, change the UX to match
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ChildItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            //TODO: This should be better and identify which nodes have been moved or changed
            //      so that we're not rebuilding the whole tree, so that the UX is not interrupted.
            //      It is, after all, all available on this event's arguments.

            //rebuild tree
            Dispatcher.BeginInvoke(() =>
            {
                //clear all
                Items.Clear();

                //add tree representations of all nodes
                foreach (var m in Carrier.ChildItems)
                {
                    //TODO: Genericise detection of media item vs. organisational item
                    //      (this is very speicific to the only provider we have right now)
                    if (m!=null && m.CurrentItem!=null && m.CurrentItem.Properties!=null && m.CurrentItem.Properties.ContainsKey("ContentTypeAlias") && String.Equals(m.CurrentItem.Properties["ContentTypeAlias"], "Folder", StringComparison.InvariantCultureIgnoreCase))
                    {
                        this.Items.Add(new MediaTreeViewItem(m));
                    }
                }

            });
        }

        protected virtual void OnExpanded(object sender, RoutedEventArgs e)
        {
            if (!Carrier.ChildrenRetreivedFromDataSource)
            {
                Carrier.PopulateChildrenFromDataSource();
            }


            OpsContext.DebugWrite("Expand");

        }



        //private void removeLoadingItem()
        //{
        //    for (int i = 0; i < this.Items.Count; i++)
        //    {
        //        Object o = this.Items[i];
        //        if (o.GetType().Name.Equals("LoadingTreeViewItem"))
        //        {
        //            this.Items.Remove(o);
        //        }
        //    }
        //}

        //protected MediaTreeView getTree()
        //{
        //    object o = this.Parent;
        //    while (o.GetType().Name.Equals("MediaTreeViewItem"))
        //    {
        //        o = ((MediaTreeViewItem)o).Parent;
        //    }
        //    MediaTreeView tree = (MediaTreeView)o;
        //    return tree;
        //}
    }
}
